from django.contrib.auth.models import User
from django.db import models

from utils.modelsMixin import ModelSetMixin

# Create your models here.
class area(models.Model):
    name = models.CharField(max_length=30,verbose_name='名称')
    parent = models.ForeignKey('self',on_delete=models.CASCADE,null=True,blank=True,verbose_name='上级行政区域')#关联自己
    class Meta:
        db_table = 'area'
        verbose_name = '行政区域'
        verbose_name_plural = verbose_name

# related_name的使用，在models.py使用Foreign定义外键的时候也可以传入一个参数related_name 是查询的时候需要的
# 这里不加上related_name会报错的原因就是省市和区都关联了同一个表，会导致反向访问器的冲突
class Address(ModelSetMixin):
    TYPE_CHOICES = (
        (0, '家'),
        (1, '公司'),
        (2,'学校'),
    )
    CHECK_CHOICES = (
        (0,'默认地址'),
        (1,'非默认地址'),
    )
    name = models.CharField(max_length=20,verbose_name='收货人')
    type = models.IntegerField(default=0,verbose_name='地址标签',choices=TYPE_CHOICES)
    province = models.ForeignKey(area,on_delete=models.PROTECT,verbose_name='省',related_name='province_address')
    city = models.ForeignKey(area,on_delete=models.PROTECT,verbose_name='市',related_name='city_address')
    district = models.ForeignKey(area,on_delete=models.PROTECT,verbose_name='区',related_name='distict_address')
    place = models.CharField(max_length=50,verbose_name='详细地址')
    is_check = models.IntegerField(default=1,verbose_name='是否默认',choices=CHECK_CHOICES)
    phone = models.CharField(max_length=11,verbose_name='手机号')

    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    class Meta:
        ordering = ['-update_time']
        db_table = 'Address'
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name

